package Misc;

import java.io.File;
import java.util.Random;
/**
 * functions often used in the system
 * @author Rupert
 *
 */
public class Functions {
	
	/**
	 * Delete a directory and all sub directories and containing files!
	 * @param path
	 * @return
	 */
	static public boolean deleteDirectory(File path) {
	    if( path.exists() ) {
	      File[] files = path.listFiles();
	      for(int i=0; i<files.length; i++) {
	         if(files[i].isDirectory()) {
	           deleteDirectory(files[i]);
	         }
	         else {
	           files[i].delete();
	         }
	      }
	    }
	    return( path.delete() );
	}
	
	/**
	 * used to generate a Random String
	 * 
	 * @param allowedChars - a String containing all allowed characters
	 * @param random 
	 * @return
	 */
	public static String generateRandomString(String allowedChars) {
		int max = allowedChars.length();
		
		Random random = new Random();
		
		StringBuffer buffer = new StringBuffer();
		for (int i=0; i<8; i++) {
			int value = random.nextInt(max);
			buffer.append(allowedChars.charAt(value));
		}
		return buffer.toString();
	}
	
	/**
	 * function to round a double value to 2 float numbers
	 * @param roundingValue
	 * @return
	 */
	public static double roundDoubleToTwoFloatNumber(double roundingValue){
		
		roundingValue = ((int) (roundingValue * 100))/ (double) 100;
		
		return roundingValue;
	}
	
	
	
}
